﻿using Vboot.Core.Module.Sys;

namespace Vboot.Core.Framework;

//数据库初始化入口
public class DbInitListener : ITransient
{
    private readonly SysOrgInit _orgInit;
    private readonly SysPortalInit _portalInit;
    private readonly SysApiInit _apiInit;
    private readonly AssInit _assInit;
    private readonly SqlSugarRepository<SysOrg> _sysOrgRepo;

    public DbInitListener(SysOrgInit orgInit,
        SysPortalInit portalInit,
        AssInit assInit,
        SysApiInit apiInit,
        SqlSugarRepository<SysOrg> sysOrgRepo)
    {
        _orgInit = orgInit;
        _portalInit = portalInit;
        _assInit = assInit;
        _apiInit = apiInit;
        _sysOrgRepo = sysOrgRepo;
    }

    //首次启动，数据库生成后，初始化组织架构，菜单，权限角色,接口等信息
    public async Task Init()
    {
        var sysOrg= _sysOrgRepo.GetSingle(it => it.id == "sa");
        if (sysOrg == null)
        {
            Console.WriteLine("首次启动系统，正在进行数据库初始化，请耐心等待。");
            await _orgInit.InitAllDept();
            Console.WriteLine("1.1 初始化部门完毕");
            await _orgInit.InitAllUser();
            await _orgInit.InitZsf();
            await _orgInit.InitSa();
            Console.WriteLine("1.2 初始化用户完毕");
            await _portalInit.initPortal();
            await _portalInit.InitSysMenu();
            await _portalInit.InitSaMenu();
            Console.WriteLine("2 初始化门户完毕");
            await _assInit.InitData();
            Console.WriteLine("3 初始化辅助数据完毕");
        }
        await _apiInit.Init();
    }

}